package com.example.zzl.adapter.SearchFragment;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.example.zzl.R;
import com.example.zzl.app.MyApplication;
import com.example.zzl.model.bean.SearchFragment.SearchResultData;
import com.example.zzl.utils.LruImageCache;

import java.util.ArrayList;

/**
 * Created by zhang on 2017/2/10.
 */

public class GridViewDWAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<SearchResultData.ResultBean.FilterListBean.ListHotTagBean> list;
    private ImageLoader imageLoader;
    private int flag = 0;

    public GridViewDWAdapter(Context context, ArrayList<SearchResultData.ResultBean.FilterListBean.ListHotTagBean> list) {
        this.context = context;
        this.list = list;
        imageLoader = new ImageLoader(MyApplication.queue, LruImageCache.getInstance());
    }


    public void select(int postion){
        flag = postion;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if(list.get(position) != null){
            return 0;
        }else if(list.get(position) == null){
            return 1;
        }
        return super.getItemViewType(position);
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder1 vh1;
        ViewHolder2 vh2;

        switch(getItemViewType(i)){
            case 0:
                if(view == null){
                    view = View.inflate(context, R.layout.gv_dw_item_1,null);
                    vh1 = new ViewHolder1();
                    vh1.iv = (ImageView)view.findViewById(R.id.iv_gv_dw);
                    view.setTag(vh1);
                }else{
                    vh1 = (ViewHolder1) view.getTag();
                }
                loadImgByVolley(list.get(i).getStrTagImage(),vh1.iv);
                if(flag == i){
                    vh1.iv.setBackgroundResource(R.drawable.dw_gv_select_shap);

                }else{
                    vh1.iv.setBackgroundColor(Color.WHITE);
                }
                break;
            case 1:

                if(view == null){
                    view = View.inflate(context, R.layout.gv_dw_item_2,null);
                    vh2 = new ViewHolder2();
                    vh2.tv = (TextView) view.findViewById(R.id.tv_gv_dw);
                    view.setTag(vh2);
                }else{
                    vh2 = (ViewHolder2) view.getTag();
                }
                vh2.tv.setText("所有商城");
                break;
        }

        return view;
    }

    class ViewHolder1{
        ImageView iv;
    }

    class ViewHolder2{
        TextView tv;
    }

    private void loadImgByVolley(String string, ImageView ivImage) {
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(ivImage,
                R.drawable.ic_launcher, R.drawable.ic_launcher);
        imageLoader.get(string, listener);
    }
}
